package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * 医院设置表 前端控制器
 *
 * @author atguigu
 * @since 2022-09-19
 */
@Slf4j
@RestController
@RequestMapping("/admin/hospitalset")
@Api(description = "医院设置接口")
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;

    @PostMapping("/add")
    @ApiOperation("医院设置添加")
    public R add(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+new Random().nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @DeleteMapping("/removeBatch")
    @ApiOperation("医院设置批量删除")
    public R removeBatch(@RequestBody List<Long> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("医院根据id查询信息")
    public R detail(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("hospitalSet",hospitalSet);
    }

    @PutMapping("/update")
    @ApiOperation("根据ID修改医院设置")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation("根据ID修改医院设置状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public R updateStatus(@PathVariable Long id , @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation("分页查询")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public R page(@RequestBody HospitalQueryVo HospitalQueryVo , @PathVariable("pageNum") Long pageNum , @PathVariable("pageSize") Long pageSize){
        Page<HospitalSet> page = new Page<>(pageNum,pageSize);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(HospitalQueryVo.getHosname()),"hosname",HospitalQueryVo.getHosname());
        queryWrapper.eq(StringUtils.isNotBlank(HospitalQueryVo.getHoscode()),"hoscode",HospitalQueryVo.getHoscode());
        hospitalSetService.page(page,queryWrapper);
        return R.ok().data("total",page.getTotal()).data("recodes",page.getRecords());
    }

    @ApiOperation(value = "医院设置列表")
    @GetMapping("/all")
    public R findAll(){

        try {
            int a = 1/0;
        }catch (Exception e){
            log.debug("debug");
            throw new YyghException(20002,"预约挂号异常");
        }
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items",list);
    }



    @DeleteMapping("/removeById/{id}")
    @ApiOperation(value = "医院设置删除")
    public R removeById(@ApiParam(name = "id",value = "医院设置id" ,required = true) @PathVariable Long id){
        boolean remove = hospitalSetService.removeById(id);
        if (remove){
            return R.ok();
        }
        return R.error();
    }

//    @ApiImplicitParam(name = "id",value = "医院设置id" ,required = true,dataType = "Long",paramType = "path")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "医院设置id" ,required = true,dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "name",value = "医院名称name" ,required = true,dataType = "String",paramType = "body")
    })
    @DeleteMapping("/deleted2/{id}")
    public R deleted2(@PathVariable Long id ,String name){
        boolean remove = hospitalSetService.removeById(id);
        if (remove){
            return R.ok();
        }
        return R.error();
    }
}

